/**
 * Created by hezhijie on 2016/11/22.
 */
var global = {
    $rootScope: null,
    addRequestExtendData: function (data) {
        if (data) {
            data["TOKEN"] = this.getToken();
        }
    },
    getToken: function () {
        return global.$rootScope.$cookieStore.get("TOKEN");
    }
}

function errorFn(reponse) {
    showMessage(reponse.responseJSON.message)
    console.log("error")
    if (reponse.responseJSON.code == 2002) {
        global.$rootScope.$state.go("login");
    }
}

$post = function (url, data, callback, eFn) {
    global.addRequestExtendData(data)
    var $p = $.post(url, serializeRequestParam(data), callback, "json");
    if (eFn) {
        $p.error(eFn);
    } else {
        $p.error(errorFn);
    }
    return $p;
}
$get = function (url, data, callback, eFn) {
    var $p;
    global.addRequestExtendData(data)
    $p = $.get(url, serializeRequestParam(data), callback, "json");
    if (eFn) {
        $p.error(eFn);
    } else {
        $p.error(errorFn);
    }
    return $p;
}

function showMessage(message) {
    alert(message)
}

function serializeRequestParam(searchObject) {
    if (searchObject && searchObject.conditions) {
        var tmpList = [];
        if (searchObject.pageNum != undefined) {
            tmpList.push("pageNum=" + 1);
        }

        if (searchObject.pageSize != undefined) {
            tmpList.push("pageSize=" + 20);
        }
        if (searchObject.conditions) {
            for (var i = 0, len = searchObject.conditions.length; i < len; i += 1) {
                var enable = false;
                tmpList.push("conditions[" + i + "].name=" + searchObject.conditions[i].colName);
                if (searchObject.conditions[i].value != undefined) {
                    tmpList.push("conditions[" + i + "].value=" + searchObject.conditions[i].value);
                    if (searchObject.conditions[i].pattern != undefined) {
                        tmpList.push("conditions[" + i + "].pattern=" + searchObject.conditions[i].pattern);
                    } else {
                        tmpList.push("conditions[" + i + "].pattern=LIKE");
                    }
                    if (!searchObject.conditions[i].valueType && !isEmpty(searchObject.conditions[i].valueType)) {
                        tmpList.push("conditions[" + i + "].valueType=" + searchObject.conditions[i].valueType);
                    } else {
                        tmpList.push("conditions[" + i + "].valueType=STRING");
                    }
                    enable = true;
                }
            }
        }
        if (searchObject.order) {
            for (var i = 0, len = searchObject.order.length; i < len; i += 1) {
                if (searchObject.order[i].colName) {
                    if (searchObject.order[i].orderSeq) {
                        tmpList.push("order[" + i + "].orderSeq=" + searchObject.order[i].orderSeq);
                    }
                }
            }
        }
        tmpList.push("TOKEN=" + searchObject.TOKEN)
        return tmpList.join("&");
    } else {
        return searchObject;
    }
}

function isEmpty(value) {
    return !value || !value.length;
}

var transference = {
    transferenceObj: {
        orderStatus: {
            "1": "待处理",
            "2": "支付成功"
        }
    },

    getText: function (name, key) {
        try {
            if (this.transferenceObj[name][key]) {
                return this.transferenceObj[name][key];
            } else {
                return ""
            }
        } catch (e) {
            return ""
        }
    }
}
